﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars;
using Moonlite.ExtensionApi.XimlInterfaces;
using Moonlite.Platform.General;

namespace Moonlite.Platform.Ximl
{
    public class ContextMenuObject : IContextMenuObject
    {
        private PopupMenu menu;
        private DescriptorBase descriptor;
        public Type DesiredDescriptorType
        {
            get { return InterfaceTypes.ContextMenuDescriptorType; }
        }
        public PopupMenu Menu
        {
            get { return menu; }
        }
        public DescriptorBase Descriptor
        {
            get { return descriptor; }
        }
        private void AddBarItem(BarItemDescriptor barItemDescriptor, PopupMenu menu)
        {
            BarItem button = null;
            if (barItemDescriptor.Children.Count > 0)
            {
                button = new BarLinkContainerItem();
                button.Caption = barItemDescriptor.Text;
            }
            else
            {
                button = new BarButtonItem(((BarManager)((object[])(Global.Instance.GetService(typeof(BarManager))))[0]), barItemDescriptor.Text, -1);
            }
            if (button != null && button is BarLinkContainerItem)
            {
                foreach (DescriptorBase childDescriptor in barItemDescriptor.Children)
                {
                    if (childDescriptor is BarItemDescriptor)
                    {
                        AddBarItem((BarItemDescriptor)childDescriptor, (BarLinkContainerItem)button);
                    }
                }
            }
            if (button != null)
            {
                button.Tag = barItemDescriptor;
                button.ItemClick += XimlEventHandler.OnButtonClicked;
                BarItemLink link = null;
                for (int i = 0; i < menu.LinksPersistInfo.Count; i++)
                {
                    if (i == barItemDescriptor.Index)
                    {
                        link = menu.LinksPersistInfo[i].Link;
                        break;
                    }
                }
                if (link == null)
                {
                    menu.AddItem(button).BeginGroup = barItemDescriptor.Separator;
                }
                else
                {
                    menu.InsertItem(link, button).BeginGroup = barItemDescriptor.Separator;
                }
            }
        }
        private void AddBarItem(BarItemDescriptor barItemDescriptor, BarLinkContainerItem barItem)
        {
            BarItem button = null;
            if (barItemDescriptor.Children.Count > 0)
            {
                button = new BarLinkContainerItem();
                button.Caption = barItemDescriptor.Text;
            }
            else
            {
                button = new BarButtonItem(((BarManager)((object[])(Global.Instance.GetService(typeof(BarManager))))[0]), barItemDescriptor.Text, -1);
            }
            if (button != null && button is BarLinkContainerItem)
            {
                foreach (DescriptorBase childDescriptor in barItemDescriptor.Children)
                {
                    if (childDescriptor is BarItemDescriptor)
                    {
                        AddBarItem((BarItemDescriptor)childDescriptor, (BarLinkContainerItem)button);
                    }
                }
            }
            button.Tag = barItemDescriptor;
            button.ItemClick += XimlEventHandler.OnButtonClicked;
            BarItemLink link = null;
            for (int i = 0; i <= barItem.LinksPersistInfo.Count; i++)
            {
                if (i == barItemDescriptor.Index)
                {
                    link = barItem.LinksPersistInfo[i].Link;
                    break;
                }
            }
            if (link == null)
            {
                barItem.AddItem(button).BeginGroup = barItemDescriptor.Separator;
            }
            else
            {
                barItem.InsertItem(link, button).BeginGroup = barItemDescriptor.Separator;
            }
        }
        public void Create(DescriptorBase descriptor)
        {
            if (Global.Instance == null || Global.Instance.GetService(typeof(BarManager)) == null || descriptor == null) return;
            ContextMenuDescriptor menuDescriptor = (ContextMenuDescriptor)descriptor;
            this.descriptor = descriptor;
            if (!XimlTree.Instance.IsNativeElement(menuDescriptor.Guid.ToString()))
            {
                menu = new PopupMenu(((BarManager)((object[])(Global.Instance.GetService(typeof(BarManager))))[0]));
            }
            else
            {
                menu = (PopupMenu)menuDescriptor.Tag;
                if (menu == null)
                {
                    return;
                }
            }
            foreach (DescriptorBase child in menuDescriptor.Children)
            {
                if (child is BarItemDescriptor)
                {
                    BarItemDescriptor barItemDescriptor = (BarItemDescriptor)child;
                    AddBarItem(barItemDescriptor, menu);
                }
            }
        }
    }
}
